import { Controller, Get, Post, Body, Patch, Param, Delete, UseGuards, HttpCode } from '@nestjs/common';
import { AuthService } from './auth.service';
import { CreateAuthDto } from './dto/create-auth.dto';
import { UpdateAuthDto } from './dto/update-auth.dto';
import { User as UserModel } from '@prisma/client';
import { CODE } from 'src/common/base';
import { returnFail, returnSuccess } from 'src/common/response';
import { AuthGuard } from '@nestjs/passport';

@Controller('/api/auth')
export class AuthController {
  constructor(private readonly authService: AuthService) {}


  // 注册用户
  @Post('/login')
  @UseGuards(AuthGuard('local'))
  @HttpCode(200)
  async login(@Body() userReq: UserModel) {
    const res = await this.authService.login(userReq);
    if (res.sql) {
      return returnFail(CODE.SQLERROR, res.msg)
    }
    return returnSuccess(res.msg, res.data)
  }
  // 登录用户
  @UseGuards(AuthGuard('local'))
  @Post('/register')
  @HttpCode(200)
  async register(@Body() userReq: UserModel) {
    const res = await this.authService.create(userReq);
    if (res.sql) {
      return returnFail(CODE.SQLERROR, res.msg)
    }
    return returnSuccess(res.msg, res.data)
  }
}
